﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mogre;

namespace WastedMechanics
{
    public abstract class Door : SelectableObject
    {
        public enum DoorState
        {
            Opening,
            Closing,
            Idle
        }

        public bool IsOpen;
        public bool IsLocked;
        public DoorState State = DoorState.Idle;
        public Quaternion BaseOrientation = Quaternion.IDENTITY;
        public Vector3 BasePosition = Vector3.ZERO;

        public event Func<Boolean> OpenPredicate;

        public bool Open()
        {            
            if (IsOpen == false)
            {                
                bool unlocked = true;
                if (IsLocked)
                {
                    if (OpenPredicate != null)
                        foreach (Delegate cond in OpenPredicate.GetInvocationList())
                            unlocked &= (bool)cond.DynamicInvoke(null);
                    else
                        unlocked = false;
                }

                if (unlocked && State == DoorState.Idle)
                {                    
                    State = DoorState.Opening;
                    return true;
                }
            }
            return false;
        }

        public bool Close()
        {
            if (IsOpen == true)
            {
                if (State == DoorState.Idle)
                {
                    State = DoorState.Closing;
                    return true;
                }
            }
            return false;
        }
       
    }
}
